/**
 * Created by caosh on 2018/1/25 0025.
 */
var AllProduct = new Vue({
    el: '#all-product',
    data: {
        num: 1,
        product: {}, // 用于复制
        navBarUlNums: [],
        navBarSelected: '',
        saleCategorieSelected: '',
        searchValue: '',
        selected: 'all',
        showCity: false,
        allCategories: false,
        isShowMask: false
    },
    methods: {
        // 跳转查询页面
        toSearch: function () {
            window.location.href='../search/index.html';
        },
        // 显示/隐藏规格
        toShowNorm: function (p) {
            p.showNorm = !p.showNorm;
        },
        showAllCategories: function () {
            this.allCategories = !this.allCategories;
            if (this.showAllCategories) {
                this.getNavBarUlNums(5);
            } else {
                this.getNavBarUlNums(4);
            }
        },
        getNavBarUlNums: function (n) {
            this.navBarUlNums = [];
            // 分类所占栏目数
            for(var i=0; i<Math.ceil(this.productCategories.length/n); i++) {
                this.navBarUlNums.push(i);
            }
        },
        search: function () {
            this.getProductsByCategorie(this.allProducts, this.navBarSelected);
            this.getProductsBySaleCategorie(this.saleCategorieSelected);
        },
        initProduct: function (p, n) {
            this.product = this.getProductFromPool(p, n);
            this.product.unitPrice = n.price;
            this.product.unit = n.unit;
            this.product.nId = n.id;
            this.num = this.product.num;
        },
        confirmProduct: function (NO) {
            this.product.num = NO;
            if (NO > 0 && this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }

            this.triggerRemoveProduct(NO, this.product);
        },
        clickReduce: function (p, n) {
            this.numObj[p.id+'-'+n.id]--;
            this.confirmProduct(this.numObj[p.id+'-'+n.id]);
        },
        clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }

            if (!n.hasOwnProperty('showControl')) {
                Vue.set(n, 'showControl', true);
            }

            /*var NO = this.numObj[p.id+'-'+n.id];
             if (!NO) {
             this.numObj[p.id+'-'+n.id] = 1;
             } else {
             this.numObj[p.id+'-'+n.id]++;
             }*/

            this.initProduct(p, n);
            var NO = this.numObj[p.id+'-'+n.id];
            if (!NO && NO != 0) {
                this.numObj[p.id+'-'+n.id] = this.num;
            } else {
                this.numObj[p.id+'-'+n.id]++;
            }
            this.confirmProduct(this.numObj[p.id+'-'+n.id]);

            // this.changeMask(p, n);
            n.click = !n.click;
        },
        /*clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.changeMask(p, n);
            n.click = !n.click;
        },*/
        // 隐藏 / 显示 遮罩层
        changeMask: function (p, n) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                this.initProduct(p, n);
            }
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            this.confirmProduct(this.num);
            this.numObj[this.product.id+'-'+this.product.nId] = this.num;
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        },
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            if (this.num > 0) {
                this.num--;
            }
            // this.triggerRemoveProduct(this.num, this.product);
        },
        toShowDetails: function (p) {
            this.showDetails(p);
            window.location.href = '../details/index.html';
        },
        selectNavBar: function (c, i) {
            this.navBarSelected = c;
            this.allCategories = false;
            setTimeout(function () {
                $('.nav-bar>.container').animate({left: (-i*100)+'%'}, 10);
            }, 0);
        }
    },
    mixins: [SystemMixin, ProductMixin, CartMixin],
    mounted: function () {
        this.getNavBarUlNums(5);
        this.getConsumerHotline();
        this.navBarSelected = this.productCategories[0];
        this.saleCategorieSelected = this.saleCategories[0];
        this.search();
        // 获得购物车数据
        this.getCartProducts();
    },
    watch: {
        selected: function (val, oldVal) {
            if (val != oldVal) { // tabbar切换
                if (val == 'home') {
                    window.location.href = '../index.html';
                } else if (val == 'cart') {
                    window.location.href = '../cart/index.html';
                } else if (val == 'mine') {
                    window.location.href = '../mine/index.html';
                }
            }
        },
        navBarSelected: function (val, oldVal) {
            if (val != oldVal) {
                this.search();
                this.saleCategories = this.scs[val];
                this.saleCategorieSelected = this.saleCategories[0];
            }
        },
        saleCategorieSelected: function (val, oldVal) {
            if (val != oldVal) {
                this.search();
            }
        },
        num: function (val, oldVal) {
            if (val <= 0 && val != '') {
                this.num = 1;
            }
        }
    }
});